
$(function() {
    validateRule();

    $("#btnSubmit").on("click",function () {
        if ($.validate.form()) {
            register()
        }
    });

    common.initOnkeydown(function () {
        if ($.validate.form()) {
            register()
        }
    })

});

$.validator.setDefaults({
    submitHandler: function() {
    	register();
    }
});

function register() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var account = $.common.trim($("input[name='account']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var companyName = $.common.trim($("input[name='companyName']").val());
    //对电子邮件的验证
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(!myreg.test(account)){
        $.modal.msgError('请输入有效的E_mail！');
        $("input[name='account']").focus();
        return ;
    }

    $.ajax({
        type: "post",
        url: ctx + "sys/user/register",
        data: {
            "account": account,
            "password": password,
            "validateCode": validateCode,
            "companyName":companyName
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
            	layer.alert("<font color='red'>恭喜你，您的账号 " + account + " 注册成功！</font>", {
            	    icon: 1,
            	    title: "系统提示"
            	},
            	function(index) {
            	    //关闭弹窗
            	    layer.close(index);
            	    location.href = ctx + 'login';
            	});
            } else {
            	$.modal.closeLoading();
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#registerForm").validate({
        rules: {
            /*account: {
                required: true,
                minlength: 2,
                email:true,
            },*/
            password: {
                required: true,
                minlength: 8
            },
            confirmPassword: {
                required: true,
                equalTo: "[name='password']"
            },
            validateCode:{
                required: true,
                minlength: 6,
                maxlength: 6
            },
            companyName:{
                required: true,
                minlength: 2,
                maxlength: 64
            }
        },
        messages: {
            password: {
            	required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于8个字符",
            },
            confirmPassword: {
                required: icon + "请再次输入您的密码",
                equalTo: icon + "两次密码输入不一致"
            }
        }
    })
}

function sendEmail() {
    var account = $.common.trim($("input[name='account']").val());

    //对电子邮件的验证
    var myreg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    if(!myreg.test(account)){
        $.modal.msgError('请输入有效的E_mail！');
        $("input[name='account']").focus();
        return ;
    }

    $.ajax({
        type: "get",
        url: ctx + "e/mail/send",
        data: {
            "to": account
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
                $.modal.msg("已发送");
            } else {
                $.modal.closeLoading();
                $.modal.msg(r.msg);
            }
        }
    });

}